Framework.Graph = {}

function Framework.Graph.Bars( ST_Label, TA_Stack, NU_X, NU_Y, NU_W, NU_H, OP_FU_Comparison, OP_FU_Add, OP_FU_Divide )
	local Max = Framework.Utilities.Round( TA_Stack:High( OP_FU_Comparison ), 2 )
	local Min = Framework.Utilities.Round( TA_Stack:Low(  OP_FU_Comparison ), 2 )
	
	local Diff = Max - Min
	local Size = TA_Stack:GetSize()
	local W = NU_W - 70
	local H = NU_H - 25
	for k, v in pairs( TA_Stack:List() ) do
		surface.DrawRect( 5 + ( NU_X + W ) - ( k * ( W / Size ) ), 5 + NU_Y + NU_H - ((v - Min)/Diff * H), math.max( 1, math.floor(W / Size) - 1 ), (v - Min)/Diff * H )
	end		
	local Y = NU_Y + NU_H - ((TA_Stack:Average() - Min)/Diff * H)
	
	surface.SetDrawColor( 255, 100, 100, 255 )
	surface.DrawLine( NU_X, Y, NU_X + W, Y )
	
	draw.RoundedBox( 4, NU_X, NU_Y + 8, NU_W, NU_H, Color( 50, 50, 50, 100 ) )
	
	surface.SetTextPos( NU_X + 5, NU_Y )
	surface.DrawText( ST_Label )
	
	surface.SetTextPos( NU_X + W + 5, NU_Y + 10 )
	surface.DrawText( Framework.Utilities.Round( Max, 2 ) )
	
	surface.SetTextPos( NU_X + W + 5, Y )
	surface.DrawText( Framework.Utilities.Round( TA_Stack:Average(), 2 ) )
	
	surface.SetTextPos( NU_X + W + 5, NU_Y + NU_H - 18 )
	surface.DrawText( Framework.Utilities.Round( Min, 2 ) )
end

local PieColors = {
	Color( 0xCC, 0x00, 0x00 ),
	Color( 0x99, 0xFF, 0x00 ),
	Color( 0xFF, 0xCC, 0x00 ),
	Color( 0x33, 0x33, 0xFF ),
	Color( 0x09, 0x70, 0x54 ),
	Color( 0xFF, 0xDE, 0x00 ),
	Color( 0x65, 0x99, 0xFF ),
	Color( 0xFF, 0x99, 0x00 )
}

function Framework.Graph.Pie( ST_Label, TA_Values, NU_X, NU_Y, NU_W, NU_H )
	
	draw.RoundedBox( 4, NU_X, NU_Y + 8, NU_W, NU_H, Color( 50, 50, 50, 100 ) )
	
	local Sum = 0
	local StrWidth, StrHeight = 0, 0
	for K, Val in pairs( TA_Values ) do
		Sum = Sum + Val
		local w, h = surface.GetTextSize( K )
		StrWidth  = math.max( w, StrWidth )
		StrHeight = math.max( h, StrHeight )
	end
	
	local RadiusX = math.min( ((NU_W - (StrWidth + 80)) / 2)*1.1, ((NU_H - 20) / 2) )
	local RadiusY = RadiusX * 0.909 --(NU_H - 20) / 2
	
	local CurDegrees = -90
	local Center = { x = (NU_X + RadiusX )+10, y = (NU_Y + NU_H / 2 ) + 10 }
	local Counter = 0
	
	surface.SetTexture( false )
	for Key, Value in pairs( TA_Values ) do
		Counter = Counter + 1
		surface.SetDrawColor( PieColors[Counter].r, PieColors[Counter].g, PieColors[Counter].b, 100 )
		surface.SetTextColor( PieColors[Counter].r, PieColors[Counter].g, PieColors[Counter].b, 200 )
		
		local Vertices = { Center }
		local Degrees = CurDegrees + (( Value / Sum ) * 360)
		
		for i = CurDegrees, Degrees, 2 do
			local x = math.cos( math.rad( i ) ) * RadiusX
			local y = math.sin( math.rad( i ) ) * RadiusY
			table.insert( Vertices, { x = Center.x + x, y = Center.y + y } )
		end
		
		local x = math.cos( math.rad( Degrees ) ) * RadiusX
		local y = math.sin( math.rad( Degrees ) ) * RadiusY
		table.insert( Vertices, { x = Center.x + x, y = Center.y + y } )
		
		CurDegrees = Degrees
		
		surface.DrawPoly( Vertices )
		table.insert( Vertices, Center )
		surface.SetDrawColor( 255, 255, 255, 255 )
		for k, v in pairs( Vertices ) do
			if( k == #Vertices ) then break end
			surface.DrawLine( v.x, v.y, Vertices[k+1].x, Vertices[k+1].y )
		end
		
		surface.SetTextPos( NU_X + NU_X + RadiusX*2, NU_Y + Counter * StrHeight )
		surface.DrawText( Key )
	
		surface.SetTextPos( NU_X + NU_W - 50, NU_Y + Counter * StrHeight )
		surface.DrawText( Framework.Utilities.Round(Value/Sum*100, 0) .. "%" )
		
	end
	
	surface.SetTextColor( 255, 255, 255, 255 )
	surface.SetTextPos( NU_X + 5, NU_Y )
	surface.DrawText( ST_Label )

end